package com.enjia.enjiashop;

import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;

import com.enjia.common.framework.BaseFragmentActivity;
import com.enjia.common.utils.CommonUtils;
import com.enjia.diary.ui.DiaryFragment;
import com.enjia.home.ui.HomeFragment;
import com.enjia.mine.ui.MineFragment;
import com.enjia.shopping.ui.ShoppingFragment;

import butterknife.BindView;

public class MainActivity extends BaseFragmentActivity implements View.OnClickListener{

    //底部单选按钮
    @BindView(R.id.btn_home)
    RadioButton btn_home;
    @BindView(R.id.btn_diary)
    RadioButton  btn_diary;
    @BindView(R.id.btn_shopping)
    RadioButton btn_shopping;
    @BindView(R.id.btn_mine)
    RadioButton btn_mine;


    //首页 Fragment
    public HomeFragment homeFragment;
    //日记 Fragment
    public DiaryFragment diaryFragment;
    //购物车 Fragment
    public ShoppingFragment shoppingFragment;
    //我的 Fragment
    public MineFragment mineFragment;


    /*****************************************************************************************************/

    FragmentManager fm = this.getSupportFragmentManager();
    private static final String SHOW_INDEX = "index";
    //记录当前Fragment显示
    private int currentPosition = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //设置底部按钮的图片大小
        initBottomSize();

        btn_home.setOnClickListener(this);
        btn_diary.setOnClickListener(this);
        btn_shopping.setOnClickListener(this);
        btn_mine.setOnClickListener(this);
        int showIndex = getIntent().getIntExtra(SHOW_INDEX, 1);
        setShowIndex(showIndex);

    }

    @Override
    public void onClick(View v) {
        FragmentTransaction ft = fm.beginTransaction();
        switch (v.getId()){
            //首页
            case R.id.btn_home:
                if (currentPosition == 1) {
                    return;
                }

                hideFragment();
                currentPosition = 1;
                if(null == homeFragment){
                    homeFragment=new HomeFragment();
                    ft.add(R.id.content,homeFragment);
                } else {
                    ft.show(homeFragment);
                }
                break;
            //日记
            case R.id.btn_diary:
                if (currentPosition == 2) {
                    return;
                }
                hideFragment();
                currentPosition = 2;
                if(null == diaryFragment){
                    diaryFragment=new DiaryFragment();
                    ft.add(R.id.content,diaryFragment);
                } else {
                    ft.show(diaryFragment);
                }
                break;
            //购物车
            case R.id.btn_shopping:
                if (currentPosition == 3) {
                    return;
                }
                hideFragment();
                currentPosition = 3;
                if(null == shoppingFragment){
                    shoppingFragment=new ShoppingFragment();
                    ft.add(R.id.content,shoppingFragment);
                } else {
                    ft.show(shoppingFragment);
                }
                break;
            //我的
            case R.id.btn_mine:
                if (currentPosition == 4) {
                    return;
                }

                hideFragment();
                currentPosition = 4;
                if(null == mineFragment){
                    mineFragment=new MineFragment();
                    ft.add(R.id.content,mineFragment);
                } else {
                    ft.show(mineFragment);
                }
                break;
        }
        ft.commit();

    }

    /**
     * 隐藏全部Fragment
     */
    private void hideFragment(){
        FragmentTransaction transaction = fm.beginTransaction();
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (diaryFragment != null) {
            transaction.hide(diaryFragment);
        }
        if (shoppingFragment != null) {
            transaction.hide(shoppingFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
        transaction.commit();
    }

    /**
     * 设置展示首页面
     */
    public void setShowIndex(int showIndex){
        if (showIndex == 1) {
            //默认展示首页
            btn_home.performClick();
        } else if (showIndex == 2) {
            // 展示日记
            btn_diary.performClick();
        } else if (showIndex == 3) {
            // 展示购物车
            btn_shopping.performClick();
        } else if (showIndex == 4) {
            // 展示我的tab
            btn_mine.performClick();
        }
    }

    //控制底部按钮图片的大小
    private void initBottomSize(){
        setTextDrawTop(btn_home,R.drawable.select_mine);
        setTextDrawTop(btn_diary,R.drawable.select_diary);
        setTextDrawTop(btn_shopping,R.drawable.select_shopping);
        setTextDrawTop(btn_mine,R.drawable.select_mine);
    }

    /**
     *
     * @param text  TextView主题
     * @param resourceID  资源图片
     */
    private void setTextDrawTop(RadioButton text, int resourceID){
        //班级名称
        Drawable drawable =getResources().getDrawable(resourceID);
        drawable.setBounds(0,0, CommonUtils.dp2px(this,24),CommonUtils.dp2px(this,24));
        text.setCompoundDrawables(null,drawable,null,null);
    }



}
